<?php

namespace App\Http\Controllers\Admin\Client;

use App\Http\Controllers\Controller;
use App\Http\Requests\client\MsgRequest;
use App\Models\client\MsgModel;
use App\Models\CustomerModel;
use Illuminate\Http\Request;

class Msg extends Controller
{
    use MsgRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $title = $request->input('title', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = MsgModel::query()->with('create_admin');

        if($title){
            $query = $query->where('title', 'like', '%'.$title.'%');
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }


        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'title' => $item->title,
                'customer_count' => count(explode(',', $item->customer_ids)),
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new MsgModel();
        $model->title = $params['title'];
        $model->customer_ids = implode(',', $params['customer_ids']);
        $model->content = $params['content'];
        $model->file = isset($params['file']) ? $params['file'] : '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function detail(Request $request, $id){
        $model = MsgModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $res = [
            'title' => $model->title,
            'created_at' => $model->created_at->toDateTimeString(),
            'file' => $model->file,
            'content' => $model->content,
        ];

        $customer_ids = explode(',', $model->customer_ids);

        $items = CustomerModel::with('customer_type')->whereIn('id', $customer_ids)->get();

        $customer_list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'name' => $item->name,
                'code' => $item->sn,
                'customer_type' => $item->customer_type ? $item->customer_type->name : '',
            ];
            return $res;
        });

        $res['customer_list'] = $customer_list;

        return $this->success($res);
    }


}
